#!/usr/bin/env python3
import argparse
import sys
import socket
import random
from scapy.all import sendp, get_if_list, get_if_hwaddr
from scapy.all import Ether, IP, IPv6, UDP, TCP

def get_if():
    ifs = [i for i in get_if_list() if i.startswith(('eth', 'en'))]
    return ifs[0] if ifs else exit("Error: No interface found")

def main():
    if len(sys.argv) != 4:
        print("Usage: sudo python3 send.py <src_ip> <dst_ip> <message>")
        exit(1)

    src, dst, msg = sys.argv[1], sys.argv[2], sys.argv[3]
    iface = get_if()
    port = 1234  # 固定端口

    # 自动选择协议（优先UDP）
    if ':' in dst:  # IPv6
        pkt = Ether(src=get_if_hwaddr(iface), dst='33:33:00:00:00:01') / \
              IPv6(src=f"{src}%{iface}", dst=f"{dst}%{iface}") / \
              UDP(dport=port, sport=random.randint(49152,65535)) / msg
    else:  # IPv4
        pkt = Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff') / \
              IP(dst=dst) / \
              UDP(dport=port, sport=random.randint(49152,65535)) / msg

    print(f"Sending to {dst} via {iface}:")
    pkt.show()
    sendp(pkt, iface=iface, verbose=False)

if __name__ == '__main__':
    main()
